<!DOCTYPE html>
<html lang="ja">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="タイニーシェイクスピアデータセットによる RETRO モデルのトレーニング"/>

    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:image:src" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta name="twitter:title" content="レトロトレーニング"/>
    <meta name="twitter:description" content="タイニーシェイクスピアデータセットによる RETRO モデルのトレーニング"/>
    <meta name="twitter:site" content="@labmlai"/>
    <meta name="twitter:creator" content="@labmlai"/>

    <meta property="og:url" content="https://nn.labml.ai/transformers/retro/train.html"/>
    <meta property="og:title" content="レトロトレーニング"/>
    <meta property="og:image" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta property="og:site_name" content="レトロトレーニング"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="レトロトレーニング"/>
    <meta property="og:description" content="タイニーシェイクスピアデータセットによる RETRO モデルのトレーニング"/>

    <title>レトロトレーニング</title>
    <link rel="shortcut icon" href="/icon.png"/>
    <link rel="stylesheet" href="../../pylit.css?v=1">
    <link rel="canonical" href="https://nn.labml.ai/transformers/retro/train.html"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css" integrity="sha384-zTROYFVGOfTw7JV7KUu8udsvW2fx4lWOsCEDqhBreBwlHI4ioVRtmIvEThzJHGET" crossorigin="anonymous">

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-4V3HC8HBLH"></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }

        gtag('js', new Date());

        gtag('config', 'G-4V3HC8HBLH');
    </script>
</head>
<body>
<div id='container'>
    <div id="background"></div>
    <div class='section'>
        <div class='docs'>
            <p>
                <a class="parent" href="/">home</a>
                <a class="parent" href="../index.html">transformers</a>
                <a class="parent" href="index.html">retro</a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations" target="_blank">
                    <img alt="Github"
                         src="https://img.shields.io/github/stars/labmlai/annotated_deep_learning_paper_implementations?style=social"
                         style="max-width:100%;"/></a>
                <a href="https://twitter.com/labmlai" rel="nofollow" target="_blank">
                    <img alt="Twitter"
                         src="https://img.shields.io/twitter/follow/labmlai?style=social"
                         style="max-width:100%;"/></a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations/tree/master/labml_nn/transformers/retro/train.py" target="_blank">
                    View code on Github</a>
            </p>
        </div>
    </div>
    <div class='section' id='section-0'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-0'>#</a>
            </div>
            <h1>レトロトレーニング</h1>
<p><a href="index.html">これはRETROのトレーニングコードです</a>。</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">14</span><span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="lineno">15</span><span class="kn">from</span> <span class="nn">torch</span> <span class="kn">import</span> <span class="n">nn</span>
<span class="lineno">16</span><span class="kn">from</span> <span class="nn">torch.utils.data</span> <span class="kn">import</span> <span class="n">DataLoader</span><span class="p">,</span> <span class="n">RandomSampler</span>
<span class="lineno">17</span>
<span class="lineno">18</span><span class="kn">from</span> <span class="nn">labml</span> <span class="kn">import</span> <span class="n">monit</span><span class="p">,</span> <span class="n">lab</span><span class="p">,</span> <span class="n">tracker</span><span class="p">,</span> <span class="n">experiment</span><span class="p">,</span> <span class="n">logger</span>
<span class="lineno">19</span><span class="kn">from</span> <span class="nn">labml.logger</span> <span class="kn">import</span> <span class="n">Text</span>
<span class="lineno">20</span><span class="kn">from</span> <span class="nn">labml_helpers.datasets.text</span> <span class="kn">import</span> <span class="n">TextFileDataset</span>
<span class="lineno">21</span><span class="kn">from</span> <span class="nn">labml_nn.optimizers.noam</span> <span class="kn">import</span> <span class="n">Noam</span>
<span class="lineno">22</span><span class="kn">from</span> <span class="nn">labml_nn.transformers.retro</span> <span class="kn">import</span> <span class="n">model</span> <span class="k">as</span> <span class="n">retro</span>
<span class="lineno">23</span><span class="kn">from</span> <span class="nn">labml_nn.transformers.retro.dataset</span> <span class="kn">import</span> <span class="n">Dataset</span><span class="p">,</span> <span class="n">RetroIndex</span>
<span class="lineno">24</span><span class="kn">from</span> <span class="nn">labml_nn.transformers.retro.model</span> <span class="kn">import</span> <span class="n">RetroModel</span><span class="p">,</span> <span class="n">NearestNeighborEncoder</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-1'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-1'>#</a>
            </div>
            <h2>サンプラー</h2>
<p>このクラスはモデルから貪欲にサンプリングします。</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">27</span><span class="k">class</span> <span class="nc">Sampler</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-2'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-2'>#</a>
            </div>
            <ul><li><code  class="highlight"><span></span><span class="n">device</span></code>
モデルのデバイスです</li>
<li><code  class="highlight"><span></span><span class="n">model</span></code>
<a href="retro.html">レトロモードです</a></li>
<li><code  class="highlight"><span></span><span class="n">tds</span></code>
テキストデータセット (隣接チャンクの取得に使用)</li>
<li><code  class="highlight"><span></span><span class="n">chunk_len</span></code>
チャンクの長さです</li></ul>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">34</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">device</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">device</span><span class="p">,</span> <span class="n">model</span><span class="p">:</span> <span class="n">retro</span><span class="o">.</span><span class="n">RetroModel</span><span class="p">,</span> <span class="n">tds</span><span class="p">:</span> <span class="n">TextFileDataset</span><span class="p">,</span> <span class="n">chunk_len</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-3'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-3'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">41</span>        <span class="bp">self</span><span class="o">.</span><span class="n">chunk_len</span> <span class="o">=</span> <span class="n">chunk_len</span>
<span class="lineno">42</span>        <span class="bp">self</span><span class="o">.</span><span class="n">tds</span> <span class="o">=</span> <span class="n">tds</span>
<span class="lineno">43</span>        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
<span class="lineno">44</span>        <span class="bp">self</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">device</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-4'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-4'>#</a>
            </div>
            <p><a href="database.html">レトロインデックス</a></p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">47</span>        <span class="bp">self</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">RetroIndex</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-5'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-5'>#</a>
            </div>
            <h3>与えられたチャンクの最も近い近傍のデータを取得</h3>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">49</span>    <span class="k">def</span> <span class="nf">retrieve_nearest_neighbours</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chunk</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-6'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-6'>#</a>
            </div>
            <p>最も近い近傍のオフセットを取得</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">55</span>        <span class="n">neighbor_offsets</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">([</span><span class="n">chunk</span><span class="p">],</span> <span class="kc">None</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-7'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-7'>#</a>
            </div>
            <p>近傍を取得 (近傍の長さがと等しい) <code  class="highlight"><span></span><span class="n">chunk_len</span> <span class="o">*</span> <span class="mi">2</span></code>
</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">58</span>        <span class="n">text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tds</span><span class="o">.</span><span class="n">train</span>
<span class="lineno">59</span>        <span class="n">neighbors</span> <span class="o">=</span> <span class="p">[</span><span class="n">text</span><span class="p">[</span><span class="n">j</span><span class="p">:</span> <span class="n">j</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">chunk_len</span> <span class="o">*</span> <span class="mi">2</span><span class="p">]</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="n">neighbor_offsets</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-8'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-8'>#</a>
            </div>
            <p></p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">62</span>        <span class="k">return</span> <span class="n">neighbors</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-9'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-9'>#</a>
            </div>
            <h3>与えられたプロンプトのサンプルテキスト</h3>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">64</span>    <span class="k">def</span> <span class="nf">sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prompt</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">sample_len</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-10'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-10'>#</a>
            </div>
            <p>最も近い近傍を文字列として保存するには</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">70</span>        <span class="n">neighbors_str</span> <span class="o">=</span> <span class="p">[]</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-11'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-11'>#</a>
            </div>
            <p>サンプルテキスト</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">73</span>        <span class="n">sampled</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-12'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-12'>#</a>
            </div>
            <p><code  class="highlight"><span></span><span class="n">sample_len</span></code>
サンプルトークン</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">76</span>        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">sample_len</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-13'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-13'>#</a>
            </div>
            <p>すでに取得したチャンクの数よりもサンプリングされたチャンクの数が多い場合は、隣接データを取得する必要があります</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">79</span>            <span class="k">while</span> <span class="nb">len</span><span class="p">(</span><span class="n">neighbors_str</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">prompt</span><span class="p">)</span> <span class="o">//</span> <span class="bp">self</span><span class="o">.</span><span class="n">chunk_len</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-14'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-14'>#</a>
            </div>
            <p>隣接データを取得していない最後のチャンクを取得</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">81</span>                <span class="n">off</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">neighbors_str</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">chunk_len</span>
<span class="lineno">82</span>                <span class="n">chunk</span> <span class="o">=</span> <span class="n">prompt</span><span class="p">[</span><span class="n">off</span><span class="p">:</span> <span class="n">off</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">chunk_len</span><span class="p">]</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-15'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-15'>#</a>
            </div>
            <p>最も近い近傍を検索する</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">84</span>                <span class="n">neighbors_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">retrieve_nearest_neighbours</span><span class="p">(</span><span class="n">chunk</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-16'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-16'>#</a>
            </div>
            <p>入力をトークン化</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">87</span>            <span class="n">src</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tds</span><span class="o">.</span><span class="n">text_to_i</span><span class="p">(</span><span class="n">prompt</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-17'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-17'>#</a>
            </div>
            <p>取得したネイバーをトークン化する</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">89</span>            <span class="n">neighbors</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">stack</span><span class="p">([</span><span class="n">torch</span><span class="o">.</span><span class="n">stack</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">tds</span><span class="o">.</span><span class="n">text_to_i</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">chunk</span><span class="p">])</span> <span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">neighbors_str</span><span class="p">])</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-18'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-18'>#</a>
            </div>
            <p>モデルと同じデバイスに移動します</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">92</span>            <span class="n">src</span> <span class="o">=</span> <span class="n">src</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">)</span>
<span class="lineno">93</span>            <span class="n">neighbors</span> <span class="o">=</span> <span class="n">neighbors</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-19'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-19'>#</a>
            </div>
            <p>モデル出力を取得</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">96</span>            <span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">src</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:],</span> <span class="n">neighbors</span><span class="p">[</span><span class="kc">None</span><span class="p">,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="p">:])</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-20'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-20'>#</a>
            </div>
            <p>最後のトークンを欲張ってサンプリングする</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">99</span>            <span class="n">token</span> <span class="o">=</span> <span class="n">res</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="p">:]</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">dim</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-21'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-21'>#</a>
            </div>
            <p>サンプリングしたトークンのテキストをプロンプトとサンプルテキストに追加します</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">102</span>            <span class="n">prompt</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tds</span><span class="o">.</span><span class="n">itos</span><span class="p">[</span><span class="n">token</span><span class="o">.</span><span class="n">item</span><span class="p">()]</span>
<span class="lineno">103</span>            <span class="n">sampled</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tds</span><span class="o">.</span><span class="n">itos</span><span class="p">[</span><span class="n">token</span><span class="o">.</span><span class="n">item</span><span class="p">()]</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-22'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-22'>#</a>
            </div>
            <p></p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">106</span>        <span class="k">return</span> <span class="n">sampled</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-23'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-23'>#</a>
            </div>
            <h2>レトロトレーナー</h2>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">109</span><span class="k">class</span> <span class="nc">Trainer</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-24'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-24'>#</a>
            </div>
            <ul><li><code  class="highlight"><span></span><span class="n">device</span></code>
モデルのデバイスです</li>
<li><code  class="highlight"><span></span><span class="n">model</span></code>
<a href="retro.html">レトロモードです</a></li>
<li><code  class="highlight"><span></span><span class="n">dataloader</span></code>
は、<a href="dataset.html">事前に近傍が取得されたデータセットのデータローダー</a></li>
<li><code  class="highlight"><span></span><span class="n">optimizer</span></code>
オプティマイザーです</li></ul>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">114</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">device</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">device</span><span class="p">,</span> <span class="n">model</span><span class="p">:</span> <span class="n">retro</span><span class="o">.</span><span class="n">RetroModel</span><span class="p">,</span>
<span class="lineno">115</span>                 <span class="n">dataloader</span><span class="p">:</span> <span class="n">DataLoader</span><span class="p">,</span> <span class="n">optimizer</span><span class="p">:</span> <span class="n">torch</span><span class="o">.</span><span class="n">optim</span><span class="o">.</span><span class="n">Optimizer</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-25'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-25'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">122</span>        <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span> <span class="o">=</span> <span class="n">optimizer</span>
<span class="lineno">123</span>        <span class="bp">self</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">device</span>
<span class="lineno">124</span>        <span class="bp">self</span><span class="o">.</span><span class="n">dataloader</span> <span class="o">=</span> <span class="n">dataloader</span>
<span class="lineno">125</span>        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
<span class="lineno">126</span>        <span class="bp">self</span><span class="o">.</span><span class="n">loss_func</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">CrossEntropyLoss</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-26'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-26'>#</a>
            </div>
            <h3>一時代を先取りしたモデルのトレーニング</h3>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">128</span>    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-27'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-27'>#</a>
            </div>
            <p>トレーニングデータを繰り返し処理</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">134</span>        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">tgt</span><span class="p">,</span> <span class="n">neighbors</span><span class="p">)</span> <span class="ow">in</span> <span class="n">monit</span><span class="o">.</span><span class="n">enum</span><span class="p">(</span><span class="s1">&#39;Train&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataloader</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-28'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-28'>#</a>
            </div>
            <p>データをデバイスに移動</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">136</span>            <span class="n">src</span><span class="p">,</span> <span class="n">tgt</span><span class="p">,</span> <span class="n">neighbors</span> <span class="o">=</span> <span class="n">src</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">),</span> <span class="n">tgt</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">),</span> <span class="n">neighbors</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-29'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-29'>#</a>
            </div>
            <p>フォワードパス</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">139</span>            <span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">neighbors</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-30'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-30'>#</a>
            </div>
            <p>損失の計算</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">141</span>            <span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss_func</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">res</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="n">tgt</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-31'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-31'>#</a>
            </div>
            <p>グラデーションをクリア</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">144</span>            <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">zero_grad</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-32'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-32'>#</a>
            </div>
            <p>バックワードパス</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">146</span>            <span class="n">loss</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-33'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-33'>#</a>
            </div>
            <p>モデルを最適化</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">148</span>            <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">step</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-34'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-34'>#</a>
            </div>
            <p>トレーニング統計を保存してグローバルステップカウンタを増やす</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">151</span>            <span class="n">tracker</span><span class="o">.</span><span class="n">save</span><span class="p">({</span><span class="s1">&#39;loss.train&#39;</span><span class="p">:</span> <span class="n">loss</span><span class="p">})</span>
<span class="lineno">152</span>            <span class="n">tracker</span><span class="o">.</span><span class="n">add_global_step</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">src</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-35'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-35'>#</a>
            </div>
            <h2>小型モデルの作成とトレーニング</h2>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">155</span><span class="k">def</span> <span class="nf">train</span><span class="p">():</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-36'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-36'>#</a>
            </div>
            <p>テストを作成</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">161</span>    <span class="n">experiment</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;retro_small&#39;</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-37'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-37'>#</a>
            </div>
            <p>GPU デバイス</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">164</span>    <span class="n">device</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">device</span><span class="p">(</span><span class="s1">&#39;cuda:0&#39;</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-38'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-38'>#</a>
            </div>
            <p>タイニーシェイクスピアデータセットの読み込み</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">167</span>    <span class="n">tds</span> <span class="o">=</span> <span class="n">TextFileDataset</span><span class="p">(</span>
<span class="lineno">168</span>        <span class="n">lab</span><span class="o">.</span><span class="n">get_data_path</span><span class="p">()</span> <span class="o">/</span> <span class="s1">&#39;tiny_shakespeare.txt&#39;</span><span class="p">,</span>
<span class="lineno">169</span>        <span class="nb">list</span><span class="p">,</span>
<span class="lineno">170</span>        <span class="n">url</span><span class="o">=</span><span class="s1">&#39;https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt&#39;</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-39'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-39'>#</a>
            </div>
            <p><a href="dataset.html">レトロデータセットの読み込み</a></p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">173</span>    <span class="n">train_dataset</span> <span class="o">=</span> <span class="n">Dataset</span><span class="p">(</span><span class="n">lab</span><span class="o">.</span><span class="n">get_data_path</span><span class="p">()</span> <span class="o">/</span> <span class="s1">&#39;retro_train_dataset.json&#39;</span><span class="p">,</span> <span class="n">tds</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-40'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-40'>#</a>
            </div>
            <p>データローダーの作成</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">176</span>    <span class="n">train_dl</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">train_dataset</span><span class="p">,</span>
<span class="lineno">177</span>                          <span class="n">batch_size</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
<span class="lineno">178</span>                          <span class="n">sampler</span><span class="o">=</span><span class="n">RandomSampler</span><span class="p">(</span><span class="n">train_dataset</span><span class="p">,</span> <span class="n">replacement</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-41'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-41'>#</a>
            </div>
            <p>ハイパーパラメータ</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">181</span>    <span class="n">chunk_len</span> <span class="o">=</span> <span class="mi">16</span>
<span class="lineno">182</span>    <span class="n">d_model</span> <span class="o">=</span> <span class="mi">128</span>
<span class="lineno">183</span>    <span class="n">d_ff</span> <span class="o">=</span> <span class="mi">512</span>
<span class="lineno">184</span>    <span class="n">n_heads</span> <span class="o">=</span> <span class="mi">16</span>
<span class="lineno">185</span>    <span class="n">d_k</span> <span class="o">=</span> <span class="mi">16</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-42'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-42'>#</a>
            </div>
            <p>最近傍エンコーダーの作成</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">188</span>    <span class="n">nearest_neighbor_encoder</span> <span class="o">=</span> <span class="n">NearestNeighborEncoder</span><span class="p">(</span><span class="n">chunk_len</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="p">{</span><span class="mi">3</span><span class="p">},</span> <span class="n">d_model</span><span class="p">,</span> <span class="n">n_heads</span><span class="p">,</span> <span class="n">d_k</span><span class="p">,</span> <span class="n">d_ff</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-43'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-43'>#</a>
            </div>
            <p>モデルを作成</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">190</span>    <span class="n">model</span> <span class="o">=</span> <span class="n">RetroModel</span><span class="p">(</span><span class="n">tds</span><span class="o">.</span><span class="n">n_tokens</span><span class="p">,</span> <span class="n">d_model</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span>
<span class="lineno">191</span>                       <span class="p">{</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">},</span>
<span class="lineno">192</span>                       <span class="n">chunk_len</span><span class="p">,</span> <span class="n">n_heads</span><span class="p">,</span> <span class="n">d_k</span><span class="p">,</span> <span class="n">d_ff</span><span class="p">,</span>
<span class="lineno">193</span>                       <span class="n">encoder</span><span class="o">=</span><span class="n">nearest_neighbor_encoder</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-44'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-44'>#</a>
            </div>
            <p>モデルをデバイスに移動</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">195</span>    <span class="n">model</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">device</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-45'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-45'>#</a>
            </div>
            <p>オプティマイザーの作成</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">197</span>    <span class="n">optimizer</span> <span class="o">=</span> <span class="n">Noam</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">parameters</span><span class="p">(),</span> <span class="n">lr</span><span class="o">=</span><span class="mf">1.</span><span class="p">,</span> <span class="n">d_model</span><span class="o">=</span><span class="n">d_model</span><span class="p">,</span> <span class="n">warmup</span><span class="o">=</span><span class="mi">2_000</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-46'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-46'>#</a>
            </div>
            <p>作成 <code  class="highlight"><span></span><span class="n">Trainer</span></code>
</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">199</span>    <span class="n">trainer</span> <span class="o">=</span> <span class="n">Trainer</span><span class="p">(</span><span class="n">device</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">train_dl</span><span class="p">,</span> <span class="n">optimizer</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-47'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-47'>#</a>
            </div>
            <p>作成 <code  class="highlight"><span></span><span class="n">Sampler</span></code>
</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">201</span>    <span class="n">sampler</span> <span class="o">=</span> <span class="n">Sampler</span><span class="p">(</span><span class="n">device</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">tds</span><span class="p">,</span> <span class="n">chunk_len</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-48'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-48'>#</a>
            </div>
            <p></p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">203</span>    <span class="n">prompt</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;Second Citizen:</span><span class="se">\n</span><span class="s1">One word, good citizens.</span><span class="se">\n\n</span><span class="s1">First Citizen:&#39;&#39;&#39;</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-49'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-49'>#</a>
            </div>
            <p>保存および読み込み用のモデルを設定する</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">206</span>    <span class="n">experiment</span><span class="o">.</span><span class="n">add_pytorch_models</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-50'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-50'>#</a>
            </div>
            <p>実験を始める</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">209</span>    <span class="k">with</span> <span class="n">experiment</span><span class="o">.</span><span class="n">start</span><span class="p">():</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-51'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-51'>#</a>
            </div>
            <p><code  class="highlight"><span></span><span class="mi">32</span></code>
時代に合わせた列車</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">211</span>        <span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="n">monit</span><span class="o">.</span><span class="n">loop</span><span class="p">(</span><span class="mi">32</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-52'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-52'>#</a>
            </div>
            <p>列車</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">213</span>            <span class="n">trainer</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-53'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-53'>#</a>
            </div>
            <p>新しい行を印刷</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">215</span>            <span class="n">tracker</span><span class="o">.</span><span class="n">new_line</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-54'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-54'>#</a>
            </div>
            <p>からのサンプル <code  class="highlight"><span></span><span class="n">prompt</span></code>
</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">217</span>            <span class="n">logger</span><span class="o">.</span><span class="n">log</span><span class="p">([(</span><span class="n">prompt</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">n</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">),</span> <span class="n">Text</span><span class="o">.</span><span class="n">subtle</span><span class="p">),</span>
<span class="lineno">218</span>                        <span class="p">(</span><span class="n">sampler</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">prompt</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">n</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">),</span> <span class="n">Text</span><span class="o">.</span><span class="n">none</span><span class="p">)])</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-55'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-55'>#</a>
            </div>
            <p>モデルを保存する</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">220</span>            <span class="n">experiment</span><span class="o">.</span><span class="n">save_checkpoint</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-56'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-56'>#</a>
            </div>
            <p></p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">224</span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
<span class="lineno">225</span>    <span class="n">train</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='footer'>
        <a href="https://papers.labml.ai">Trending Research Papers</a>
        <a href="https://labml.ai">labml.ai</a>
    </div>
</div>
<script src=../../interactive.js?v=1"></script>
<script>
    function handleImages() {
        var images = document.querySelectorAll('p>img')

        for (var i = 0; i < images.length; ++i) {
            handleImage(images[i])
        }
    }

    function handleImage(img) {
        img.parentElement.style.textAlign = 'center'

        var modal = document.createElement('div')
        modal.id = 'modal'

        var modalContent = document.createElement('div')
        modal.appendChild(modalContent)

        var modalImage = document.createElement('img')
        modalContent.appendChild(modalImage)

        var span = document.createElement('span')
        span.classList.add('close')
        span.textContent = 'x'
        modal.appendChild(span)

        img.onclick = function () {
            console.log('clicked')
            document.body.appendChild(modal)
            modalImage.src = img.src
        }

        span.onclick = function () {
            document.body.removeChild(modal)
        }
    }

    handleImages()
</script>
</body>
</html>